/*
 *  ckr_spec_nullpointer_comparisons.cpp
 *  ckrSpec
 *
 *  Created by C. Keith Ray on 2006.11.06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */

#import "ckr_spec_cpp_specs.h"
#import "ckr_spec_cpp.h"

#include <iostream>
#include <sstream>
#include <vector>
#include <deque>

#include <assert.h>

using namespace std;
using namespace ckr;

static void pointer_not_null_in_should_be_null_reports_failure();
static void pointer_null_in_should_be_null_reports_nothing();

static void pointer_null_in_should_not_be_null_reports_failure();
static void pointer_not_null_in_should_be_not_null_reports_nothing();

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void run_nullpointer_comparisons_specs()
{
	pointer_not_null_in_should_be_null_reports_failure();
	pointer_null_in_should_be_null_reports_nothing();

	pointer_null_in_should_not_be_null_reports_failure();
	pointer_not_null_in_should_be_not_null_reports_nothing();
}

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DECLARE_SPEC(Pointer_not_null_in_should_be_null);

void Pointer_not_null_in_should_be_null::spec()
{
	int * ptr = (int*)(0x1010);

	POINTER( ptr ).SHOULD_BE_NULL; // fail msg expected
}

static void pointer_not_null_in_should_be_null_reports_failure()
{
	Pointer_not_null_in_should_be_null context;
	
	RecordingFailuresSpecListener listener;
	context.run_spec( & listener );

	std::string actual = listener.logStr();
	
	const std::string expected("ptr (0x1010) should be null FAILED in Pointer_not_null_in_should_be_null; line");

	std::string::size_type foundPos = actual.find( expected, 0 );
	assert( foundPos != std::string::npos );	
}

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DECLARE_SPEC(Pointer_null_in_should_be_null);

void Pointer_null_in_should_be_null::spec()
{
	int * ptr = NULL;

	POINTER( ptr ).SHOULD_BE_NULL; // no msg expected
}

static void pointer_null_in_should_be_null_reports_nothing()
{
	Pointer_null_in_should_be_null context;
	
	RecordingFailuresSpecListener listener;
	context.run_spec( & listener );

	std::string actual = listener.logStr();
	const std::string expected;
	
	assert( expected == actual );
}

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DECLARE_SPEC(Pointer_null_in_should_not_be_null);

void Pointer_null_in_should_not_be_null::spec()
{
	int * ptr = NULL;

	POINTER( ptr ).SHOULD_NOT_BE_NULL; // fail msg expected
}

static void pointer_null_in_should_not_be_null_reports_failure()
{
	Pointer_null_in_should_not_be_null context;
	
	RecordingFailuresSpecListener listener;
	context.run_spec( & listener );

	std::string actual = listener.logStr();
	
	const std::string expected("ptr (0) should not be null FAILED in Pointer_null_in_should_not_be_null; line");

	std::string::size_type foundPos = actual.find( expected, 0 );
	assert( foundPos != std::string::npos );	
}

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DECLARE_SPEC(Pointer_not_null_in_should_not_be_null);

void Pointer_not_null_in_should_not_be_null::spec()
{
	int * ptr = (int*)(0x1010);

	POINTER( ptr ).SHOULD_NOT_BE_NULL; // fail msg expected
}

static void pointer_not_null_in_should_be_not_null_reports_nothing()
{
	Pointer_not_null_in_should_not_be_null context;
	
	RecordingFailuresSpecListener listener;
	context.run_spec( & listener );

	std::string actual = listener.logStr();
	const std::string expected;
	
	assert( expected == actual );	
}

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

